package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.SkuMapper;
import com.lemeng.dao.SpuMapper;
import com.lemeng.entity.Goods;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Sku;
import com.lemeng.pojo.Spu;
import com.lemeng.service.SpuService;
import com.lemeng.util.IdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = SpuService.class)
public class SpuServiceImpl implements SpuService{

    @Autowired
    SpuMapper spuMapper;

    @Autowired
    SkuMapper skuMapper;

    @Override
    public List<Goods> findAll() {
        List<Spu> spus = spuMapper.selectAll();
        return zuzhuangListGoods(spus);
    }

    public List<Goods> zuzhuangListGoods(List<Spu> spus){
        List<Goods> goodsList = new ArrayList<>();
        if (spus!=null&&spus.size()>0){
            for (Spu spu : spus) {
                goodsList.add(zuzhuangGoods(spu));
            }
        }
        return goodsList;
    }

    public Goods zuzhuangGoods(Spu spu){
        Goods goods = new Goods();
        goods.setSpu(spu);
        Sku sku = new Sku();
        sku.setSpuId(spu.getId());
        sku.setStatus("1");
        List<Sku> skuList = skuMapper.select(sku);
        goods.setSkuList(skuList);
        return goods;
    }

    @Override
    public PageResult<Goods> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Spu> spuPageInfo = new PageInfo<>(spuMapper.selectAll());
        return new PageResult(spuPageInfo.getTotal(),zuzhuangListGoods(spuPageInfo.getList()));
    }

    @Override
    public List<Goods> findList(Map<String, Object> serchMap) {
        List<Spu> spuList = spuMapper.selectByExample(zuzhuangExample(serchMap));
        return zuzhuangListGoods(spuList);
//        return zuzhuangListGoods(spuMapper.selectByExample(zuzhuangExample(serchMap)));//一行写法
    }

    public Example zuzhuangExample(Map<String, Object> serchMap){
        Example example = new Example(Spu.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
            if (serchMap.get("sn")!=null && StringUtil.isNotEmpty(serchMap.get("sn").toString())){
                criteria.andLike("sn","%"+serchMap.get("sn").toString()+"%");
            }
            if (serchMap.get("brandId")!=null && StringUtil.isNotEmpty(serchMap.get("brandId").toString())){
                criteria.andEqualTo("brandId",serchMap.get("brandId").toString());
            }
            if (serchMap.get("category1Id")!=null && StringUtil.isNotEmpty(serchMap.get("category1Id").toString())){
                criteria.andEqualTo("category1Id",serchMap.get("category1Id").toString());
            }
            if (serchMap.get("category2Id")!=null && StringUtil.isNotEmpty(serchMap.get("category2Id").toString())){
                criteria.andEqualTo("category2Id",serchMap.get("category2Id").toString());
            }
            if (serchMap.get("category3Id")!=null && StringUtil.isNotEmpty(serchMap.get("category3Id").toString())){
                criteria.andEqualTo("category3Id",serchMap.get("category3Id").toString());
            }
        }
        return example;
    }

    @Override
    public PageResult<Goods> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);

        PageInfo<Spu> spuPageInfo = new PageInfo<>(spuMapper.selectByExample(zuzhuangExample(serchMap)));

        List<Spu> spuList = spuPageInfo.getList();
        List<Goods> goodsList = zuzhuangListGoods(spuList);
        return new PageResult(spuPageInfo.getTotal(),goodsList);
    }

    @Override
    public Goods findById(String id) {
        return zuzhuangGoods(spuMapper.selectByPrimaryKey(id));
    }

    @Autowired
    IdWorker idWorker;
    @Override
    public void add(Goods goods) {
        Spu spu = goods.getSpu();
        spu.setId(idWorker.nextId()+"");
        spu.setSaleNum(0);
        spu.setCommentNum(0);
        spu.setIsMarketable("1");
        spu.setIsEnableSpec("1");
        spu.setIsDelete("0");
        spu.setStatus("0");
        spuMapper.insert(spu);

        for (Sku sku : goods.getSkuList()) {
            sku.setId(idWorker.nextId()+"");
            sku.setName(spu.getName());
            sku.setCreateTime(new Date());
            sku.setUpdateTime(sku.getCreateTime());
            sku.setSpuId(spu.getId());
            sku.setSaleNum(0);
            sku.setCommentNum(0);
            spu.setStatus("1");
            skuMapper.insert(sku);
        }
    }

    @Override
    public void update(Goods goods) {
        //修改的过程：
        // 1、spu的修改：正常修改；
        Spu spu = goods.getSpu();
        spuMapper.updateByPrimaryKeySelective(goods.getSpu());
        // 2、skuList的修改：先删除这个spu下所有的sku，然后在加入新dkuList
        Sku skuTJ = new Sku();
        skuTJ.setSpuId(goods.getSpu().getId());
        skuMapper.delete(skuTJ);

        for (Sku sku : goods.getSkuList()) {
            sku.setId(idWorker.nextId()+"");
            sku.setName(spu.getName());
            sku.setCreateTime(new Date());
            sku.setUpdateTime(sku.getCreateTime());
            sku.setSpuId(spu.getId());
            sku.setSaleNum(0);
            sku.setCommentNum(0);
            spu.setStatus("1");
            skuMapper.insert(sku);
        }

    }

    @Override
    @Transactional
    public void delete(Integer id) {
        spuMapper.deleteByPrimaryKey(id);
        Sku sku = new Sku();
        sku.setSpuId(id+"");
        skuMapper.delete(sku);
    }

    @Override
    public void updateIsMarketable(Spu spu) {
        spuMapper.updateByPrimaryKeySelective(spu);
    }

    @Override
    public void editSkuInfo(List<Sku> skuList) {
        for (Sku sku : skuList) {
            skuMapper.updateByPrimaryKeySelective(sku);
        }
    }

}
